﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

public partial class UserControl_ControlRegisterNPP : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ViewState["flag"] == null)
            {
               // ViewState["flag"] = false;
                ClearAdd();
            }
            BindDanhHieu();

           
            
        }
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {


        ClearAdd();
    }
    private void clearDetail()
    {
        txtMaSo.Text = "New";
        txtMaRecommender.Text = "";
        txtTenRecommender.Text = "";
        txtMaRecommender.ReadOnly = false;
        txtTenRecommender.ReadOnly = false;
        txtName.Text = "";
        

        txtPhone.Text = "";
        txtSearch.Text = "";
       
      
    }
    void ClearAdd()
    {
        TabContainer1.ActiveTabIndex = 0;
        clearDetail();
        ViewState["flag"] = true;
        txtName.Focus();

        btnSave.Text = "Lưu";
        lblMsg.Text = "";
        lblMsg.Visible = false;
    }
    protected void grvNPP_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (e.CommandName == "chon")
            {

                GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                Label lblMaSo = (Label)grv.FindControl("lblMaSo");

                DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getDetailNPP", CommandType.StoredProcedure,
                    new SqlParameter("@salesp_key", lblMaSo.Text));
                txtMaSo.Text = db.Rows[0]["salesp_key"].ToString();
                txtName.Text = db.Rows[0]["salesp_name"].ToString();
                
                try
                {
                   chSex.Checked =bool.Parse(db.Rows[0]["Gender"].ToString());
                }
                catch
                {
                }
               
               
               
                try
                {
                    ddlDanhHieu.SelectedValue = db.Rows[0]["saleslevel"].ToString();
                }
                catch
                {
                }
                txtPhone.Text=db.Rows[0]["Mobile"].ToString();
	           
               
                chActive.Checked=bool.Parse(db.Rows[0]["Active"].ToString());
                txtMaRecommender.Text=db.Rows[0]["Recommender"].ToString();
                txtTenRecommender.Text=db.Rows[0]["TenRecommender"].ToString();
               
               
      
                grvNPP.DataSource = null;
                grvNPP.DataBind();

               // txtMaRecommender.ReadOnly = true;
               // txtTenRecommender.ReadOnly = true;

                ViewState["flag"] = false;
                btnSave.Text = "Cập nhật";
                lblMsg.Visible = false;

                TabContainer1.ActiveTabIndex = 0;

            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }    
    }
    protected void btSearch_Click(object sender, EventArgs e)
    {
        LoadGrid();
    } 
    void BindDanhHieu()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getHrsalelevel", CommandType.StoredProcedure);
        dt.BindDropDownList(db, ddlDanhHieu, "Rate", "Description");
    }
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }
    protected void grvNPP_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvNPP.PageIndex = e.NewPageIndex;
        LoadGrid();

    }
    void LoadGrid()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "K_FindInforNPP", CommandType.StoredProcedure,
                new SqlParameter("@parameter", txtSearch.Text));
            
            if (db.Rows.Count == 0)
            {
                grvNPP.DataSource = null;
                ShowMessageBox("Không tìm thấy kết quả");
            }
            else
            {
                grvNPP.DataSource = db;
               
            }
            grvNPP.DataBind();
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void txtMaRecommender_TextChanged(object sender, EventArgs e)
    {
        try
        {
            string Chuoi = txtMaRecommender.Text;
            int index = Chuoi.IndexOf("|");
            txtMaRecommender.Text = Chuoi.Substring(0, index - 1);
            txtTenRecommender.Text = Chuoi.Substring(index + 2, Chuoi.Length - index - 2);
            
        }
        catch
        {
            return;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
       
        int result = 0;
        try
        {
            if ((bool)ViewState["flag"])
            {
                //insert
                result = dt.ExecuteSQl_int(str, "k_insertArsalesp", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@salesp_name", txtName.Text),
                    new SqlParameter("@Gender", chSex.Checked),
                    new SqlParameter("@Mobile", txtPhone.Text),
                    new SqlParameter("@Level", ddlDanhHieu.SelectedValue.ToString()),
                    new SqlParameter("@Active", chActive.Checked),
                    new SqlParameter("@Recommender", txtMaRecommender.Text));
                if (result == 1)
                {
                    //ShowMessageBox("Thêm mới ngành hàng thành công");
                    ClearAdd();
                    lblMsg.Text = "Thêm mới nhân viên kinh doanh thành công";
                    lblMsg.Visible = true;
                }
                else
                {
                    //ShowMessageBox("Hệ thống đang bảo trì");
                    lblMsg.Text = "Hệ thống đang bảo trì";
                    lblMsg.Visible = true;
                }
            }
            else
            {
                //update
                result = dt.ExecuteSQl_int(str, "k_updateArsalesp", CommandType.StoredProcedure, ref error,
                     new SqlParameter("@salesp_name", txtName.Text),
                    new SqlParameter("@Gender", chSex.Checked),
                    new SqlParameter("@Mobile", txtPhone.Text),
                    new SqlParameter("@salesPkey", txtMaSo.Text),
                    new SqlParameter("@Level",ddlDanhHieu.SelectedValue.ToString()),
                    new SqlParameter("@Recommender",txtMaRecommender.Text));
                if (result == 1)
                {
                    //ShowMessageBox("Cập nhật ngành hàng thành công");
                    lblMsg.Text = "Cập nhật thông tin nhân viên kinh doanh thành công";
                    lblMsg.Visible = true;
                }
                else
                {
                    //ShowMessageBox("Hệ thống đang bảo trì");
                    lblMsg.Text = "Hệ thống đang bảo trì";
                    lblMsg.Visible = true;
                }
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }    
    }
}